package com.azure.database;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@MapperScan(basePackages = "com.azure.mapper.bigstore", sqlSessionTemplateRef = "storedbSqlSessionTemplate")
public class StoredbMybatisConfig {

	@Bean(name = "nodedbConfig")
	@ConfigurationProperties(prefix = "mysql.datasource.storedb")
	public BaseDBConfig dbConfig() {
		return new BaseDBConfig();
	}

	@Primary
	@Bean(name = "nodedbDataSource")
	public DataSource nodedbDataSource(@Qualifier("nodedbConfig") BaseDBConfig nodedbConfig) throws SQLException {
		System.out.println(nodedbConfig.getUrl());
		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
		mysqlXaDataSource.setUrl(nodedbConfig.getUrl());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
		mysqlXaDataSource.setPassword(nodedbConfig.getPassword());
		mysqlXaDataSource.setUser(nodedbConfig.getUsername());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);

		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(mysqlXaDataSource);
		xaDataSource.setUniqueResourceName("nodedbDataSource");

		xaDataSource.setMinPoolSize(nodedbConfig.getMinPoolSize());
		xaDataSource.setMaxPoolSize(nodedbConfig.getMaxPoolSize());
		xaDataSource.setMaxLifetime(nodedbConfig.getMaxLifetime());
		xaDataSource.setBorrowConnectionTimeout(nodedbConfig.getBorrowConnectionTimeout());
		xaDataSource.setLoginTimeout(nodedbConfig.getLoginTimeout());
		xaDataSource.setMaintenanceInterval(nodedbConfig.getMaintenanceInterval());
		xaDataSource.setMaxIdleTime(nodedbConfig.getMaxIdleTime());
		xaDataSource.setTestQuery(nodedbConfig.getTestQuery());
		return xaDataSource;
	}

	@Bean(name = "nodedbSqlSessionFactory")
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("nodedbDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath:mapper/bigstore/*.xml"));
		return bean.getObject();
	}

	@Bean(name = "storedbSqlSessionTemplate")
	public SqlSessionTemplate testSqlSessionTemplate(
			@Qualifier("nodedbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
